#include "Explosion.h"
#define TICK_PER_FRAME 200

CExplosion::CExplosion(void)
{
	_Count =0;
	_Level = 0;
	_x = -100;
	_y = -100;
	now = 0;
	_IsAlive = true;
}

CExplosion::~CExplosion(void)
{
}

void CExplosion::LoadResources()
{
	_Explosion[0] = CSpriteManager::getInstance()->GetExplosionSprite(0);
	_Explosion[1] = CSpriteManager::getInstance()->GetExplosionSprite(1);
	_Explosion[2] = CSpriteManager::getInstance()->GetExplosionSprite(2);
	_BigExplosion[0] = CSpriteManager::getInstance()->GetExplosionSprite(3);
	_BigExplosion[1] = CSpriteManager::getInstance()->GetExplosionSprite(4);
	_BigExplosion[2] = CSpriteManager::getInstance()->GetExplosionSprite(5);
}

void CExplosion::Explode(int X, int Y, int Level)
{
	_x = X;
	_y = Y;
	_Level = Level;
	_IsAlive = true;
}

void CExplosion::Render()
{
	if(_IsAlive)
	{
		switch(_Level)
		{
			case 0:
			{
				if(_Count < 3)
				{
					if(GetTickCount() - now >= TICK_PER_FRAME)
					{
						_Explosion[_Count]->Render(_x, _y, 0);
						now = GetTickCount();
						_Count++;
					}
					else
					{
						_Explosion[_Count - 1]->Render(_x, _y, 0);
					}
				}
				else
				{
					_IsAlive = false;
				}
				break;	
			}
			case 1:
			{
				if(_Count < 3)
				{
					if(GetTickCount() - now >= TICK_PER_FRAME)
					{
						_BigExplosion[_Count]->Render(_x, _y, 0);
						now = GetTickCount();
						_Count++;
					}
					else
					{
						_BigExplosion[_Count - 1]->Render(_x, _y, 0);
					}
				}
				else
				{
					_IsAlive = false;
				}
				break;
			}
			case 2:
			{
				if(_Count < 3)
				{
					if(GetTickCount() - now >= TICK_PER_FRAME)
					{
						_BigExplosion[_Count]->Render(_x, _y, 0);
						_BigExplosion[_Count]->Render(_x - 24, _y - 12, 0);
						_BigExplosion[_Count]->Render(_x + 24, _y - 12, 0);
						_BigExplosion[_Count]->Render(_x, _y + 12, 0);
						now = GetTickCount();
						_Count++;
					}
					else
					{
						_BigExplosion[_Count - 1]->Render(_x, _y, 0);
						_BigExplosion[_Count - 1]->Render(_x - 24, _y - 12, 0);
						_BigExplosion[_Count - 1]->Render(_x + 24, _y - 12, 0);
						_BigExplosion[_Count - 1]->Render(_x, _y + 12, 0);
					}
				}
				else
				{
					_IsAlive = false;
				}
				break;
			}
		}
	}
}

bool CExplosion::CheckAlive()
{
	return _IsAlive;
}

void CExplosion::Reset()
{
	_Count =0;
	_Level = 0;
	_x = -100;
	_y = -100;
	now = 0;
}